package com.ajk.server.bkcontroller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ajk.server.controller.access.ArgIndexForPrintLog;
import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.model.DepartmentDict;
import com.ajk.server.model.HospitalDict;
import com.ajk.server.model.query.DepartmentDictQuery;
import com.ajk.server.model.query.HospitalDictQuery;
import com.ajk.server.model.response.ResultList;
import com.ajk.server.model.response.ResultObject;
import com.ajk.server.service.DepartmentDictService;
import com.ajk.server.service.HospitalDictService;
import com.ajk.server.service.RegionDictService;
import com.ajk.server.utils.IntegerUtil;

/**
 * 医院 
 *
 */
@RequestMapping("/admin/hospital")
@RestController
public class AdminHospitalController {
	Logger logger = LoggerFactory.getLogger(AdminHospitalController.class);

	@Autowired
	HospitalDictService hospitalDictService;

	@Autowired
	DepartmentDictService departmentDictService;

	@Autowired
	RegionDictService regionDictService;


	/**
	 * 根据ID查询科室
	 * @param id
	 * @return
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/by_id/{id}", method = RequestMethod.GET)
	public ResultObject<DepartmentDict> fetchById(@PathVariable("id") Integer id){
		DepartmentDict department = departmentDictService.findByIdWithCache(id);
		return new ResultObject<>(department);
	}



	//================================ 管理员接口 ===================================================

	/**
	 * 添加医院-医院管理
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResultObject<Integer> addHospital(@RequestBody HospitalDict hospitalDict){
		if(StringUtils.isEmpty(hospitalDict.getName())){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"名字不能为空");
			logger.info("添加医院失败",exception);
			throw exception;
		}
		if(!IntegerUtil.isPositiveInteger(hospitalDict.getLevelId())){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"医院级别为空或不合法");
			logger.info("添加医院失败",exception);
			throw exception;
		}
		if(!IntegerUtil.isPositiveInteger(hospitalDict.getRegionId())){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"所属区域为空或不合法");
			logger.info("添加医院失败",exception);
			throw exception;
		}
		if(StringUtils.isEmpty(hospitalDict.getAddress())){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"地址不能为空");
			logger.info("添加医院失败",exception);
			throw exception;
		}
		if(hospitalDict.getLongitude()==null || hospitalDict.getLongitude()<0 || hospitalDict.getLongitude()>180){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"经度为空或不合法");
			logger.info("添加医院失败",exception);
			throw exception;
		}
		if(hospitalDict.getLatitude()==null || hospitalDict.getLatitude()<0 || hospitalDict.getLatitude()>90){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"纬度为空或不合法");
			logger.info("添加医院失败",exception);
			throw exception;
		}

		Integer id = hospitalDictService.insertHospitalDict(hospitalDict);

		return new ResultObject<>(id);

	}



	/**
	 * 修改医院-医院管理
	 * 不能修改InUsed;
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/modify/{hosptialId}", method = RequestMethod.PUT)
	public ResultObject<Integer> modifyHospital(@PathVariable("hosptialId") Integer hosptialId,@RequestBody HospitalDict hospitalDict){
		if(!IntegerUtil.isPositiveInteger(hosptialId)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的医院ID必须是正整数");
			logger.info("修改医院失败",exception);
			throw exception;
		}
		HospitalDict db = hospitalDictService.findByIdInCache(hosptialId);
		if(db==null){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"修改的医院不存在");
			logger.info("修改医院失败",exception);
			throw exception;
		}
		if(hospitalDict.getLongitude()==null &&( hospitalDict.getLongitude()<0 || hospitalDict.getLongitude()>180)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"经度不合法");
			logger.info("修改医院失败",exception);
			throw exception;
		}
		if(hospitalDict.getLatitude()!=null &&( hospitalDict.getLatitude()<0 || hospitalDict.getLatitude()>90)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"纬度不合法");
			logger.info("修改医院失败",exception);
			throw exception;
		}

		hospitalDict.setInUsed(null); //不能修改InUsed;

		hospitalDict.setId(hosptialId);

		hospitalDictService.updateHospitalDict(hospitalDict);

		return new ResultObject<>(hosptialId);
	}



	/**
	 * 禁用医院-医院管理
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/disable/{hosptialId}", method = RequestMethod.PUT)
	public ResultObject<Integer> disableHospital(@PathVariable("hosptialId") Integer hosptialId){
		if(!IntegerUtil.isPositiveInteger(hosptialId)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的医院ID必须是正整数");
			logger.info("禁用医院失败",exception);
			throw exception;
		}
		HospitalDict db = hospitalDictService.findByIdInCache(hosptialId);
		if(db==null){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"禁用医院不存在");
			logger.info("禁用医院失败",exception);
			throw exception;
		}

		db.setInUsed(0);

		hospitalDictService.updateHospitalDict(db);

		return new ResultObject<>(hosptialId);

	}



	/**
	 * 启用医院-医院管理
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/enable/{hosptialId}", method = RequestMethod.PUT)
	public ResultObject<Integer> enableHospital(@PathVariable("hosptialId") Integer hosptialId){
		if(!IntegerUtil.isPositiveInteger(hosptialId)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的医院ID必须是正整数");
			logger.info("启用医院失败",exception);
			throw exception;
		}
		HospitalDict db = hospitalDictService.findHospitalDictById(hosptialId);
		if(db==null){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"启用医院不存在");
			logger.info("启用医院失败",exception);
			throw exception;
		}

		db.setInUsed(1);

		hospitalDictService.updateHospitalDict(db);

		return new ResultObject<>(hosptialId);

	}

	/**
	 * 拉取医院列表
	 * 
	 * 直接查询数据库，管理平台速度慢些无所谓
	 * 
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResultList<HospitalDict> questHospital(HospitalDictQuery query){
		ResultList<HospitalDict> resultList = new ResultList<>();
		if(IntegerUtil.isPositiveInteger(query.getRegionId())){
			query.setRegionIds(regionDictService.findSelfAllChildrenId(query.getRegionId()));
		}

		List<HospitalDict> result = hospitalDictService.findHospitalDictByQuery(query);

		Integer count = hospitalDictService.countHospitalDictByQuery(query);

		query.setCount(count);
		resultList.setItemList(result);

		resultList.setExt(query);

		return resultList;
	}


	/**
	 * 添加科室
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/add", method = RequestMethod.POST)
	public ResultObject<Integer> addDepartment(@RequestBody DepartmentDict departmentDict){
		String name = departmentDict.getName();
		if(StringUtils.isEmpty(name)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"名字不能为空");
			logger.info("添加科室失败",exception);
			throw exception;
		}
		if(departmentDict.getParentId()==null){
			departmentDict.setParentId(0);
		}

		DepartmentDict dbByName = departmentDictService.findByName(name);
		if(dbByName!=null){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"科室已存在，不需添加");
			logger.info("添加科室失败",exception);
			throw exception;
		}

		Integer id = departmentDictService.insertDepartmentDict(departmentDict);

		return new ResultObject<Integer>(id);

	}


	/**
	 * 修改科室
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/modify/{departmentId}", method = RequestMethod.PUT)
	public ResultObject<Integer> modifyDepartment(@PathVariable("departmentId") Integer departmentId, @RequestBody DepartmentDict departmentDict){

		if(!IntegerUtil.isPositiveInteger(departmentId)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的科室ID必须是正整数");
			logger.info("修改科室失败",exception);
			throw exception;
		}
//		DepartmentDict db = departmentDictService.findByIdWithCache(departmentId);
		DepartmentDict db = departmentDictService.findById(departmentId);
		if(db==null){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"修改的科室不存在");
			logger.info("修改科室失败",exception);
			throw exception;
		}
		departmentDict.setInUsed(null);
		Integer idInteger = departmentDictService.updateDepartmentDict(departmentDict);
		return new ResultObject<Integer>(idInteger);
	}

	/**
	 * 禁用科室
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/disable/{departmentId}", method = RequestMethod.PUT)
	public ResultObject<Integer> disableDepartment(@PathVariable("departmentId") Integer departmentId){
		if(!IntegerUtil.isPositiveInteger(departmentId)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的科室ID必须是正整数");
			logger.info("禁用科室失败",exception);
			throw exception;
		}
		DepartmentDict db = departmentDictService.findByIdWithCache(departmentId);
		if(db==null){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"禁用科室不存在");
			logger.info("禁用科室失败",exception);
			throw exception;
		}

		db.setInUsed(0);

		departmentDictService.updateDepartmentDict(db);

		return new ResultObject<>(departmentId);

	}

	/**
	 * 启用科室 
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/enable/{departmentId}", method = RequestMethod.PUT)
	public ResultObject<Integer> enableDepartment(@PathVariable("departmentId") Integer departmentId){
		if(!IntegerUtil.isPositiveInteger(departmentId)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的科室ID必须是正整数");
			logger.info("启用科室失败",exception);
			throw exception;
		}
		DepartmentDict db = departmentDictService.findById(departmentId);
		if(db==null){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"启用科室不存在");
			logger.info("启用科室失败",exception);
			throw exception;
		}

		db.setInUsed(1);
		departmentDictService.updateDepartmentDict(db);

		return new ResultObject<>(departmentId);
	}


	/**
	 * 查询科室
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/list", method = RequestMethod.GET)
	public ResultList<DepartmentDict> questHospital(DepartmentDictQuery query){
		ResultList<DepartmentDict> resultList = new ResultList<>();

		List<DepartmentDict> result = departmentDictService.findDepartmentDictByQuery(query);

		Integer count = departmentDictService.countDepartmentDictByQuery(query);

		query.setCount(count);
		resultList.setItemList(result);

		resultList.setExt(query);

		return resultList;
	}
	
	/**
	 * 通过ID查询科室
	 */
	/**
	 * 启用科室 
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/{departmentId}", method = RequestMethod.GET)
	public ResultObject<DepartmentDict> findOneDepartment(@PathVariable("departmentId") Integer departmentId){
		if(!IntegerUtil.isPositiveInteger(departmentId)){
			AjkException exception = new AjkException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的科室ID必须是正整数");
			logger.info("查询科室失败",exception);
			throw exception;
		}
		DepartmentDict db = departmentDictService.findById(departmentId);
		return new ResultObject<>(db);
	}
}
